#pragma once
#include"../Log/log.hpp"
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string>
#include<iostream>
#include<errno.h>
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>

#define MAX_BACKLOG 1024
class Socket{
private:
    int sockfd_;

public:
    Socket()
    :sockfd_(-1)
    {}

    Socket(int fd)
    :sockfd_(fd)
    {}

    ~Socket()
    {
        Close();
    }

    int get_fd()
    {
        return sockfd_;
    }
    bool Create()
    {
        sockfd_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if(sockfd_ == -1)
        {
            ERR_LOG("socket fail");
            return false;
        }
        return true;
    }

    bool Bind(const std::string& ip, uint16_t port)
    {
        struct sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr.s_addr = inet_addr(ip.c_str());
        int len = sizeof(addr);

        int ret = bind(sockfd_, (const sockaddr*)&addr, (socklen_t)len);
        if(ret == -1)
        {
            ERR_LOG("bind fail");
            return false;
        }
        return true;
    }

    bool Listen(int backlog = MAX_BACKLOG)
    {
        int ret = listen(sockfd_, backlog);
        if(ret == -1)
        {
            ERR_LOG("listen fail");
            return false;
        }
        return true;
    }

    bool Connect(const std::string& ip, uint16_t port)
    {
        struct sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr.s_addr = inet_addr(ip.c_str());
        int len = sizeof(addr);

        int ret = connect(sockfd_, (const sockaddr*)&addr, (socklen_t)len);
        if(ret < 0)
        {
            ERR_LOG("connect fail");
            return false;
        }
        return true;
    }

    int Accept()
    {
        int ret = accept(sockfd_, NULL, NULL);
        if(ret < 0)
        {
            ERR_LOG("accept fail");
            return -1;
        }
        return ret;
    }

    int Recv(void* buf, size_t len, int flag = 0)
    {   
        int ret = recv(sockfd_, buf, len, flag);
        if(ret <= 0)
        {
            if(errno == EAGAIN ||errno == EINTR)
            {
                return 0;
            }
            ERR_LOG("recv fail");
            //printf("Err:%s", strerror(errno));
            return -1;
        }
        return ret;
    }

    int RecvNonblock(void* buf, size_t len)
    {
        return Recv(buf, len, MSG_DONTWAIT);
    }

    int Send(const void* buf, size_t len, int flag = 0)
    {
        int ret = send(sockfd_, buf, len, flag);
        if(ret < 0 )
        {
            if(errno == EAGAIN ||errno == EINTR)
            {
                return 0;
            }
            else
            {
                ERR_LOG("send fail");
                return -1;
            }
        }
        return ret;
    }

    int SendNonblock(const void* buf, size_t len)
    {
        return Send(buf, len, MSG_DONTWAIT);
    }

    void Close()
    {
        if(sockfd_ != -1)
        {
            close(sockfd_);
            sockfd_ = -1;
        }
    }

    //文件描述符设置为非阻塞
    void Nonblock()
    {
        int flag = fcntl(sockfd_, F_GETFL, 0);
        fcntl(sockfd_, F_SETFL, flag | O_NONBLOCK);
    }

    //地址端口复用
    void AddrReuse()
    {
        int opt = 1;
        setsockopt(sockfd_, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
        opt = 1;
        setsockopt(sockfd_, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
    }


    bool CreateServer(const std::string &ip, uint16_t port, int block_flag = true)
    {
        //设置套接字    绑定端口    监听套接字
        if(Create() == false) return false;
        AddrReuse();
        if(block_flag == false)     Nonblock();//默认是阻塞的
        if(Bind(ip, port) == false) return false;
        if(Listen() == false) return false;
        return true;
    }

    bool CreateClient(const std::string &ip, uint16_t port)
    {
        //设置套接字    连接服务器
        if(Create() == false) return false;
        if(Connect(ip, port) == false) return false;
        return true; 
    }
};